
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// Member_Config Data Class
    /// </summary>
    public partial class Member_ConfigDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Member_Config";

        public Member_ConfigDAL()
        {}

        #region  成员方法

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Save(Member_Config model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("if exists(select 0 from [Member_Config] where [MemberAccount]=@MemberAccount and [ConfigKey]=@ConfigKey) ");
            strSql.Append("update [Member_Config] set ");
            strSql.Append("[ConfigValue]=@ConfigValue,[Memo]=@Memo,[Modifier]=@Modifier,[ModifyTime]=@ModifyTime ");
            strSql.Append(" where [MemberAccount]=@MemberAccount and [ConfigKey]=@ConfigKey ");
            strSql.Append("else ");
            strSql.Append("insert into [Member_Config]( ");
            strSql.Append("[MemberAccount],[ConfigKey],[ConfigValue],[Memo],[CreateTime],[Modifier],[ModifyTime] ");
            strSql.Append(") values ( ");
            strSql.Append("@MemberAccount,@ConfigKey,@ConfigValue,@Memo,@CreateTime,@Modifier,@ModifyTime ");
            strSql.Append(")");
            
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@ConfigKey", model.ConfigKey),
                    new SqlParameter("@ConfigValue", model.ConfigValue),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@Modifier", model.Modifier),
                    new SqlParameter("@ModifyTime", model.ModifyTime)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_Config Get(String MemberAccount)
        {
            StringBuilder strSql = new StringBuilder();
            
            strSql.Append("select  top 1 * from [Member_Config] ");
            strSql.Append(" where [MemberAccount]=@MemberAccount");
            
            SqlParameter[] parameters = {
            
                new SqlParameter("MemberAccount", MemberAccount)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_Config Get(DataRow row)
        {
            if (row == null) return null;

            Member_Config model = new Member_Config();
    
            
            if (row["MemberAccount"] != DBNull.Value)
            {
                model.MemberAccount = (String)row["MemberAccount"];
            }      
            
            if (row["ConfigKey"] != DBNull.Value)
            {
                model.ConfigKey = (String)row["ConfigKey"];
            }      
            
            if (row["ConfigValue"] != DBNull.Value)
            {
                model.ConfigValue = (String)row["ConfigValue"];
            }      
            
            if (row["Memo"] != DBNull.Value)
            {
                model.Memo = (String)row["Memo"];
            }      
            
            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }      
            
            if (row["Modifier"] != DBNull.Value)
            {
                model.Modifier = (String)row["Modifier"];
            }      
            
            if (row["ModifyTime"] != DBNull.Value)
            {
                model.ModifyTime = (DateTime)row["ModifyTime"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            
            strSql.Append("select * from [Member_Config] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Member_Config] ");
            
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Member_Config] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Member_Config]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by createTime desc ", out count, cmdParms);
            total = count;
            return dt;
        }
      #endregion  成员方法
    }
}


  